/**
 * 
 */
package offer.binarytree;

import java.util.Arrays;

/**
 * @author zhang
 *
 */
public class IsBalanceTree39 {

	public static boolean isBalance(BinaryTree<Integer> head) {
		if (head == null)
			return true;
		else {
			int left = getTreeDepth(head.left);
			int right = getTreeDepth(head.right);
			if (Math.abs(left - right) > 1)
				return false;
		}
		return isBalance(head.left) && isBalance(head.right);
	}

	public static <T> int getTreeDepth(BinaryTree<T> head) {
		if (head == null)
			return 0;
		int left = getTreeDepth(head.left);
		int right = getTreeDepth(head.right);
		return left > right ? left + 1 : right + 1;
	}

	public static void main(String[] args) {
		BinaryTree<Integer> head = null;
		java.util.List<Integer> list = Arrays.asList(10, 5, 12, 4, 7, 66, 77, 333);
		head = BinaryTree.createBinaryTree(list);
		boolean balance = isBalance(head);
		System.out.println(balance);

	}

}
